﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using ERMap;

namespace PhoneBook.Manage
{
    public class Stat
    {
        /*
         表明:b_stat
         列:curdate,web,wap,ivr,mobile,isdel
         
         */
        public static Hashtable getsiglemodel(string itype)
        {
            Hashtable keys = new Hashtable();
            //keys.Add("itype", itype);
            return ERMapHelper.getCustomModel("select isnull(sum(" + itype + "),0)as total from b_stat where 1=1 ", keys);
            //Hashtable keys = new Hashtable();
            //keys.Add("total", 10);
            //return keys;
        }

        public static Hashtable getsiglemodel(string itype, string dtBeginDate, string dtEndDate)
        {
            Hashtable keys = new Hashtable();
            keys["starttime"] = dtBeginDate;
            keys["endtime"] = dtEndDate;
            //keys.Add("itype", itype);
            return ERMapHelper.getCustomModel("select isnull(sum(" + itype + "),0)as total from b_stat where curdate between @starttime and @endtime ", keys);
            //Hashtable keys = new Hashtable();
            //keys.Add("total", 10);
            //return keys;
        }

        public static void add(string itype)
        {
            string curdate = DateTime.Now.ToString("yyyyMMdd");
            Hashtable newStat = get(curdate);
            if (newStat == null)
            {
                newStat = new Hashtable();
            }
            if (!newStat.ContainsKey("curdate")) newStat["curdate"] = curdate;
            if (!newStat.ContainsKey(itype)) newStat[itype] = 0;
            newStat[itype] = Convert.ToInt64(newStat[itype]) + 1;
            if (!newStat.ContainsKey("web")) newStat["web"] = 0;
            if (!newStat.ContainsKey("wap")) newStat["wap"] = 0;
            if (!newStat.ContainsKey("ivr")) newStat["ivr"] = 0;
            if (!newStat.ContainsKey("mobile")) newStat["mobile"] = 0;
            ERMapHelper.setmodel("b_stat", "curdate", newStat);
        }
        public static Hashtable get(string curdate) 
        {
            Hashtable newstat=new Hashtable();
            newstat["curdate"]=curdate;
            return ERMapHelper.getmodel("b_stat", newstat);
        }

        public static DataSet StatGroupUserCount()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select count(g.groupid) as usercount from b_group g ");
            sb.Append(" where isnull(g.isdel,0)=0");

            return ERMapHelper.getCustomData(sb.ToString(), null);
        }

        public static DataSet StatGroupEmplCount()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select g.groupid,g.groupname,count(e.employeeid) as usercount from b_group g ");
            sb.Append(" inner join b_department d on g.groupid = d.groupid ");
            sb.Append(" inner join b_employee e on d.departmentid = e.departmentid ");
            sb.Append(" where isnull(g.isdel,0)=0 and isnull(d.isdel,0)=0 and isnull(e.isdel,0)=0 ");
            sb.Append(" group by g.groupid,g.groupname ");

            return ERMapHelper.getCustomData(sb.ToString(), null);
        }

        public static DataSet StatLinkCountByGroup(string begintime, string endtime)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select isnull(g.groupname,'运营商') as groupname,SUM(s.web) as web,SUM(s.wap) as wap,SUM(s.ivr) as ivr,SUM(s.mobile) as mobile ");
            sb.Append(" from b_stat s left outer join b_group g on s.groupid = g.groupid ");
            sb.Append(" where cast(s.curdate as datetime) between @begintime and @endtime ");
            sb.Append(" group by g.groupname ");

            Hashtable keys = new Hashtable();
            keys["begintime"] = begintime;
            keys["endtime"] = endtime;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }
    }
}
